<?php

require_once 'Zend/Loader.php';

class Bootstrap
{

    public static $frontController = null;
    public static $registry = null;

    public static function run()
    {
        self::setupEnvironment();
        Zend_Loader::registerAutoload();
        self::setupRegistry();
        self::setupConfiguration();
        self::setupFrontController();
        self::setupView();
        self::setupDatabase();
        self::setupAcl();
        
        $response = self::$frontController->dispatch();
        self::sendResponse($response);
    }

    public static function setupEnvironment()
    {
        error_reporting(E_ALL|E_STRICT);
        ini_set('display_startup_errors', true);
		ini_set('display_errors', true);
        date_default_timezone_set('Europe/Copenhagen');
    }
    
    public static function setupRegistry()
    {
    	// This allows to access the registry as a object (eg. $registry->config etc.)
    	// http://framework.zend.com/manual/en/zend.registry.html#zend.registry.using.array-object
    	self::$registry = new Zend_Registry(array(), ArrayObject::ARRAY_AS_PROPS);
		Zend_Registry::setInstance(self::$registry);
    }
    
    public static function setupConfiguration()
    {
        $config = new Zend_Config_Ini('../application/config.ini', 'general');
        self::$registry->configuration = $config;
    }

    public static function setupFrontController()
    {
        self::$frontController = Zend_Controller_Front::getInstance();
        self::$frontController->throwExceptions(true);
        self::$frontController->returnResponse(true);
        self::$frontController->setControllerDirectory(
            '../application/controllers'
        );
        self::$frontController->setParam('registry', self::$registry);
    }

    public static function setupView()
    {
        $view = new Zend_View;
        $view->setEncoding('UTF-8');
        $viewRenderer = new Zend_Controller_Action_Helper_ViewRenderer($view);
        Zend_Controller_Action_HelperBroker::addHelper($viewRenderer);
        Zend_Layout::startMvc(
            array(
                'layoutPath' => '.../application/layouts',
                'layout' => 'layout'
            )
        );
    }

    public static function setupDatabase()
    {
    	$config = self::$registry->configuration;
    	$db = Zend_Db::factory($config->db);
        $db->query("SET NAMES 'utf8'");
        
        Zend_Db_Table::setDefaultAdapter($db);
        self::$registry->database = $db;
    }

    public static function setupAcl()
    {
    	/*
        $auth = Zend_Auth::getInstance();
        $acl = new ZFBlog_Acl($auth);
        self::$frontController->setParam('auth', $auth);
        self::$frontController->setParam('acl', $acl);
        self::$frontController->registerPlugin(
            new ZFBlog_Controller_Plugin_Acl($auth, $acl)
        );
		*/
    }
    
    public static function sendResponse(Zend_Controller_Response_Http $response)
    {
        $response->setHeader('Content-Type', 'text/html; charset=UTF-8', true);
        $response->sendResponse();
    }

}